package com.ruicar.afs.cloud.filecenter.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.filecenter.entity.AfsFileChunkInfo;
import com.ruicar.afs.cloud.filecenter.entity.AfsFileInfo;
import com.ruicar.afs.cloud.filecenter.entity.AfsFileStore;
import com.ruicar.afs.cloud.filecenter.enums.ProcessStatus;
import com.ruicar.afs.cloud.filecenter.mapper.FileChunkInfoMapper;
import com.ruicar.afs.cloud.filecenter.mapper.FileStoreMapper;
import com.ruicar.afs.cloud.filecenter.service.IFileChunkService;
import com.ruicar.afs.cloud.filecenter.service.IFileInfoService;
import com.ruicar.afs.cloud.filecenter.util.MimeHelper;
import com.ruicar.afs.cloud.filecenter.vo.ChunkInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * IFileStoreService
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date create on 2020-01-02 12:06:01
 * @since 1.0
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class FileChunkService extends ServiceImpl<FileChunkInfoMapper, AfsFileChunkInfo> implements IFileChunkService {
    private final IFileInfoService fileInfoService;
    private final FileStoreMapper fileStoreMapper;
    @Override
    public ChunkInfo checkChunkInfo(String filename, String md5, int chunkNumber, int totalChunks, String guid) {
        AfsFileInfo afsFileInfo = this.fileInfoService.getOne(Wrappers.<AfsFileInfo>lambdaQuery().eq(AfsFileInfo::getGuid, guid));
        if (afsFileInfo == null) {
            afsFileInfo = new AfsFileInfo();
            afsFileInfo.setAccessKey(UUID.fastUUID().toString().replaceAll("-", ""));
            afsFileInfo.setFileName(filename);
            afsFileInfo.setFileType(FileUtil.extName(filename));
            afsFileInfo.setFileMime(MimeHelper.getMimeTypes(afsFileInfo.getFileType().toLowerCase()));
            afsFileInfo.setFileMd5(md5);
            afsFileInfo.setGuid(guid);
            fileInfoService.save(afsFileInfo);
        }
        AfsFileStore afsFileStore = fileStoreMapper.selectOne(Wrappers.<AfsFileStore>lambdaQuery().eq(AfsFileStore::getMd5Value,md5));
        if(afsFileStore!=null&&ProcessStatus.FINISH_STATUS.contains(afsFileStore.getProcessStatus())){
            return ChunkInfo.builder().
                    isFinished(Boolean.TRUE)
                    .uploadChunkNums(getAllChunkIndex(totalChunks)).index(chunkNumber).accessKey(afsFileInfo.getAccessKey()).build();
        }else {
            List<AfsFileChunkInfo> afsFileChunkInfoList = this.baseMapper.selectList(Wrappers.<AfsFileChunkInfo>lambdaQuery().eq(AfsFileChunkInfo::getChunkedId, md5));
            return ChunkInfo.builder().
                    isFinished(totalChunks == afsFileChunkInfoList.size())
                    .uploadChunkNums(afsFileChunkInfoList.stream().map(AfsFileChunkInfo::getChunkedIndex).collect(Collectors.toList())).index(chunkNumber).accessKey(afsFileInfo.getAccessKey()).build();
        }
    }

    private List<Integer> getAllChunkIndex(int totalChunks){
        List rtnList = new ArrayList();
        for(int index=1;index<=totalChunks;index++){
            rtnList.add(index);
        }
        return  rtnList;
    }
}
